package com.unusap.remote.core.util;

import com.unusap.remote.core.dto.ValueEntry;

import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

/**
 * Created by IntelliJ IDEA.
 * User: danson
 * Date: 2009-8-20
 * Time: 15:05:50
 * To change this template use File | Settings | File Templates.
 */
public abstract class ValueEntryUtils {

    public static ValueEntry getValueEntry(Collection<ValueEntry> entries, final String name) {
        return (ValueEntry) CollectionUtils.find(entries, new ValueEntryPredicate(name));
    }

    @SuppressWarnings("unchecked")
	public static Collection<ValueEntry> getValueEntries(Collection<ValueEntry> entries, final String name) {
        return (Collection<ValueEntry>) CollectionUtils.select(entries, new ValueEntryPredicate(name));
    }

    private static class ValueEntryPredicate implements Predicate {
        private final String name;

        public ValueEntryPredicate(String name) {
            this.name = name;
        }

        public boolean evaluate(Object object) {
            return name.equals(((ValueEntry) object).getName());
        }
    }
}
